﻿@{
    ViewBag.Title = "Complate";
}
<p>Send me spam:
    <input type="checkbox" onclick="vm.chkAll()" data-bind="    checked: wantsSpam" /></p>
<div>
    Preferred flavors of spam:   
    <div>
        <input type="checkbox" value="cherry" data-bind="checked: spamFlavors" onclick="vm.chkOne()" />
        Cherry
    </div>
    <div>
        <input type="checkbox" value="almond" data-bind="checked: spamFlavors" onclick="    vm.chkOne()" />
        Almond
    </div>
    <div>
        <input type="checkbox" value="msg" data-bind="checked: spamFlavors" onclick="    vm.chkOne()" />
        Monosodium Glutamate
    </div>
</div>
<script type="text/javascript">
    function viewModel() {
        var self = this;
        self.wantsSpam = ko.observable(true);
        self.spamFlavors = ko.observableArray(["cherry", "almond"]);
        self.chkAll = function () {
            setTimeout(function () {
                var all = self.wantsSpam();
                if (all == true) {
                    self.spamFlavors.removeAll();
                }
            }, 50);

        };
        self.chkOne = function () {
            setTimeout(function () {
                var all = self.spamFlavors().length;
                console.log(all);
                if (all == 3) {
                    self.spamFlavors.removeAll();
                    self.wantsSpam(true);
                }
                else {
                    self.wantsSpam(false);
                }
            }, 50);
        }



    };
    var vm = new viewModel();

    ko.applyBindings(vm);

    //viewModel.spamFlavors.push("msg"); 

</script>

